/**
 * Copyright (c) 2009, http://www.softwareinlife.com. All rights reserved.
 * 
 * Redistribution and use in source and binary forms, 
 * with or without modification, are permitted provided that 
 * the following conditions are met:
 * 
 * 1. Redistributions of source code must retain the above copyright notice, 
 *    this list of conditions and the following disclaimer.
 * 
 * 2. Redistributions in binary form must reproduce the above copyright notice, 
 *    this list of conditions and the following disclaimer in the documentation 
 *    and/or other materials provided with the distribution.
 * 
 * 3. Neither the name of the http://www.softwareinlife.com nor the names of its 
 *    contributors may be used to endorse or promote products derived from 
 *    this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE SOFTWAREINLIFE AND CONTRIBUTORS "AS IS" 
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
 * ARE DISCLAIMED. 
 * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package com.softwareinlife.vision.android.ux.plan;

import java.util.Date;

import android.app.Activity;
import android.app.DatePickerDialog.OnDateSetListener;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

import com.softwareinlife.vision.android.R;
import com.softwareinlife.vision.android.data.dao.PlanDAO;
import com.softwareinlife.vision.android.data.exceptions.DatabaseConnectionException;
import com.softwareinlife.vision.android.data.exceptions.VisionUpdateException;
import com.softwareinlife.vision.android.data.model.PlanModel;
import com.softwareinlife.vision.android.ux.UXConstants;

public class UpdatePlanActivity extends Activity{
	
	/**
	 * Plan IDs
	 */
	private int planIds = 0;

	/**
	 * Plan
	 */
	private PlanModel plan = null;

	// Member Variables //////////////////////////////

	/**
	 * Title Text View
	 */
	private EditText titleEditText = null;

	/**
	 * Statement Edit Text
	 */
	private EditText statementEditText = null;

	/**
	 * Constants for Start Date
	 */
	protected static final int START_DATE_DIALOG_ID = 1;

	/**
	 * Click Listener for Start Date
	 */
	protected OnClickListener startDateClickListener = null;

	/**
	 * Date Select Listener for Start Date
	 */
	protected OnDateSetListener startDateSelectListener = null;

	/**
	 * Constants for End Date
	 */
	protected static final int END_DATE_DIALOG_ID = 0;

	/**
	 * Click Listener for End Date
	 */
	protected OnClickListener endDateClickListener = null;

	/**
	 * Date Select Listener for End Date
	 */
	protected OnDateSetListener endDateSelectListener = null;

	// CREATE BUTTON /////////////////////////////////////

	/**
	 * Button of Create
	 */
	private Button updateButton = null;

	// CANCEL BUTTON /////////////////////////////////////

	/**
	 * Button of Cancel
	 */
	private Button cancelButton = null;

	/**
	 * Plan DAO
	 */
	private PlanDAO dao = null;

	/**
	 * On Create
	 */
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		// Set Content View
		setContentView(R.layout.plan_update);

		// Load Data
		loadData();

		// Initialize Members
		initMemembers();

		// Initialize Actions
		initActions();
	}

	/**
	 * Initialize Members
	 */
	private void initMemembers() {
		// Title
		titleEditText = (EditText) findViewById(R.id.titleEditText);
		titleEditText.setText(plan.getTitle());

		// Statement
		statementEditText = (EditText) findViewById(R.id.statmentEditText);
		statementEditText.setText(plan.getDesc());

		// Create Button
		updateButton = (Button) findViewById(R.id.updateButton);
		updateButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				try {
					updatePlanHandler();
				} catch (VisionUpdateException e) {
					e.printStackTrace();
				}
				changePlanListScreen();
			}
		});

		// Cancel Button
		cancelButton = (Button) findViewById(R.id.cancelButton);
		cancelButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				changePlanListScreen();
			}
		});
	}
	
	/**
	 * Update Vision Data
	 * @throws VisionUpdateException 
	 */
	protected void updatePlanHandler() throws VisionUpdateException {
		try {
			// Create Model
			PlanModel model = new PlanModel();
			model.setTitle(titleEditText.getText().toString());
			model.setDesc(statementEditText.getText().toString());
			
			// Now
			Date now = new Date();
			model.setModifiedDate(now);
			
			// Create Operation
			dao.update(model);
		} catch (VisionUpdateException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * Change Vision List Screen
	 */
	protected void changePlanListScreen() {
		Intent intent = new Intent(this, ReadPlanActivity.class);
		startActivity(intent);
	}

	/**
	 * Load List Data
	 */
	private void loadData() {
		// Get Intent
		Intent intent = getIntent();
		planIds = intent.getExtras().getInt(UXConstants.VISION_ID);

		// Create DAO
		try {
			dao = new PlanDAO(getApplicationContext());
		} catch (DatabaseConnectionException e) {
			e.printStackTrace();
		}
		
		// Read All
		plan = dao.read(planIds);
	}

	/**
	 * Initialize Actions
	 */
	private void initActions() {

	}
}
